using FytSoa.Application.Crm.Param;
using FytSoa.Common.Extensions;
using FytSoa.Domain.Crm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Crm;

/// <summary>
/// 联系人服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v7")]
public class CrmContactService : IApplicationService 
{
    private readonly SugarRepository<CrmContact> _thisRepository;
    public CrmContactService(SugarRepository<CrmContact> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CrmContactDto>> GetPagesAsync(CrmContactSearchParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Id!=0,m=>m.CustomerId==param.Id)
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Name.Contains(param.Key) || m.Mobile.Contains(param.Key))
            .WhereIF(!string.IsNullOrEmpty(param.IdArray),m=>param.IdArray.StrToListLong().Contains(m.Id))
            .Includes(m=>m.CustomerObj)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CrmContactDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CrmContactDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m=>m.CustomerObj)
            .FirstAsync(m=>m.Id==id);
        return model.Adapt<CrmContactDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CrmContactAdd model)
    {
        var isAny = await _thisRepository.IsAnyAsync(m => m.Name == model.Name && m.Mobile==model.Mobile);
        if (isAny)
        {
            throw new BusinessException("联系人重复了");
        }
        return await _thisRepository.InsertAsync(model.Adapt<CrmContact>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CrmContactAdd model)
    {
        var isAny = await _thisRepository.IsAnyAsync(m => m.Name == model.Name && m.Mobile==model.Mobile && m.Id!=model.Id);
        if (isAny)
        {
            throw new BusinessException("联系人重复了");
        }
        return await _thisRepository.UpdateAsync(model.Adapt<CrmContact>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
